const { Service } = require('egg');

class HomeService extends Service {

  async initDataBase() {
    try {
      const knex = this.app.knex;
      // await this.createDictionary(knex);
      const list = [
        this.createDictionary(knex),
        this.createUser(knex),
        this.createRole(knex),
        this.createUserRoleRelationship(knex),
        // this.createDepart(knex),
        // this.createDepartRoleRelationship(knex),
        this.createProduct(knex),
        this.createAddress(knex),
        this.createBarcode(knex),
        this.createPrice(knex),
        this.createInventory(knex),
        this.createInventoryApproval(knex),
        this.createInventoryApprovalProcess(knex),
        this.createOrderInfo(knex),
        this.createOrderProcess(knex),
        this.createOrderPosition(knex),
        this.createOrderProduct(knex),
        this.createFile(knex),
        this.createFileCloud(knex),
        this.createPermission(knex),
        this.createRoleAndPermission(knex),
        this.createNotice(knex),
        this.createNoticeRead(knex),
        this.createRequestHistory(knex),
      ];
      const result = await Promise.all(list);

      for (const r of result) {
        console.log(r);
      }

      return 'success';
    } catch (error) {
      throw error;
    }
  }

  // 创建数据字典表
  async createDictionary(knex) {
    const table_name = 'dictionary';
    try {
      const isExist = await knex.schema.hasTable(table_name);
      let str = `${table_name} `;
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('数据字典表');
          t.string('uuid').primary().notNullable()
            .comment('uuid');
          t.string('name').comment('名称');
          t.string('key').comment('key');
          t.string('value').comment('值');
          t.string('comment').comment('注释');
          t.integer('sort').comment('排序');
          t.string('parent_id').comment('父uuid');
          t.tinyint('is_enable').defaultTo(0).notNullable()
            .comment('是否启用 0:启用 1:未启用');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
          t.unique('key');
        });

        str += 'create success';
      } else {
        str += 'is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建用户表
  async createUser(knex) {
    const table_name = 'user';
    try {
      const isExist = await knex.schema.hasTable(table_name);
      let str = table_name;
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('用户表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('name').notNullable().comment('用户名称');
          t.string('login_name').notNullable().comment('登录名');
          t.tinyint('sex').notNullable().comment('性别; 0: 女, 1: 男');
          t.string('password').notNullable().comment('密码');
          t.string('phone').comment('电话');
          t.string('email').comment('邮箱');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
          t.unique('login_name');
        });

        const defaultValue = {
          uuid: '-1',
          name: '超级管理员',
          login_name: 'admin',
          password: '4e866485ee8bf885e4cb54b5031465f2', // Admin@2024,
          sex: 3,
          phone: '18090744712',
          email: 'gurenxujiu@163.com',
          create_by: -1,
          update_by: -1,
        };
        await knex(table_name).insert(defaultValue);
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建角色表
  async createRole(knex) {
    const table_name = 'role';
    try {
      const isExist = await knex.schema.hasTable(table_name);

      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('角色表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('name').notNullable().comment('名称');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        const defaultValue = {
          uuid: '-1',
          name: '超级管理员',
          create_by: '-1',
          update_by: '-1',
        };
        await knex(table_name).insert(defaultValue);
      }
      return `${table_name} create success`;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建用户角色关系表
  async createUserRoleRelationship(knex) {
    const table_name = 'user_role_relationship';
    try {
      const isExist = await knex.schema.hasTable(table_name);
      let str = table_name;
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('用户角色关系表');
          t.increments('id').primary().comment('id');
          t.string('user_id').notNullable().comment('用户id');
          t.string('role_id').notNullable().comment('角色id');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('id');
        });

        const defaultValue = {
          user_id: '-1',
          role_id: '-1',
          create_by: '-1',
          update_by: '-1',
        };

        await knex(table_name).insert(defaultValue);
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建地址表
  async createAddress(knex) {
    const table_name = 'address';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);

      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('地址表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('address').comment('地址表');
          t.string('user_name').comment('用户名称');
          t.string('user_phone').comment('用户电话');
          t.string('user_email').comment('用户邮箱');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建商品表
  async createProduct(knex) {
    const table_name = 'product';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);

      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('商品表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('name').notNullable().comment('商品名称');
          t.string('describe').comment('描述');
          t.string('type').notNullable().comment('类型');
          t.string('file_id').comment('文件id');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });


        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建批次表
  async createBarcode(knex) {
    const table_name = 'barcode';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('批次表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('content').notNullable().comment('批次内容');
          t.string('product_id').comment('商品id');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
          t.unique('content');
        });

        str += ' create success';
      } else {
        str += 'is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create success`;
    }
  }

  // 创建价格表
  async createPrice(knex) {
    const table_name = 'price';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('价格表');

          t.string('uuid').comment('uuid').primary();
          t.string('product_id').comment('产品id').notNullable();
          t.string('price').comment('价格');
          t.string('remark').comment('价格备注');
          t.string('type').comment('类型');
          t.datetime('end_time').comment('到期时间');
          t.timestamp('start_time').comment('开始时间');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建库存表
  async createInventory(knex) {
    const table_name = 'inventory';

    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);

      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('库存表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('product_id').notNullable().comment('商品');
          t.string('barcode_id').comment('批次');
          t.string('number').notNullable().comment('数量');
          t.tinyint('type').notNullable().comment('类型 0: 入库 1: 出库');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });


        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建库存审核表
  async createInventoryApproval(knex) {
    const table_name = 'inventory_approval';

    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('库存审批表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('barcode_id').notNullable().comment('批次');
          t.string('product_id').notNullable().comment('产品id');
          t.string('number').comment('数量');
          t.string('title').notNullable().comment('审批标题');
          t.tinyint('status').notNullable().defaultTo(0)
            .comment('审批状态');
          t.tinyint('type').comment('类型 0:入库 1:出库');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).notNullable()
            .comment('是否删除 0: 未删除, 1: 删除');
        });


        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        str += 'create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建库存审核流程表
  async createInventoryApprovalProcess(knex) {
    const table_name = 'inventory_approval_process';
    try {
      let str = table_name;

      const isExist = await knex.schema.hasTable(table_name);

      if (!isExist) {

        await knex.schema.createTable(table_name, t => {
          t.comment('库存审批记录表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('approval_id').notNullable().comment('审批id');
          t.string('title').notNullable().comment('主题');
          t.string('content').comment('内容');
          t.string('reason').comment('理由');
          t.tinyint('status').notNullable().comment('状态');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建订单表
  async createOrderInfo(knex) {
    const table_name = 'order_info';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);

      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('订单信息表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('name').notNullable().comment('名称');
          t.string('describe').comment('描述');
          t.string('barcode').comment('订单编号');
          t.string('address_id').notNullable().comment('地址');
          t.tinyint('type').notNullable().comment('类型');
          t.tinyint('level').notNullable().comment('紧急状况');
          t.tinyint('status').notNullable().comment('状态 1: 进行 2.完成 3.取消');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').notNullable().defaultTo(0)
            .comment('是否删除');
        });


        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  async createOrderProduct(knex) {
    const table_name = 'order_product';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {

        await knex.schema.createTable(table_name, t => {
          t.comment('订单产品表');

          t.string('uuid').notNullable().primary()
            .comment('uiid');
          t.string('product_id').notNullable().comment('产品id');
          t.string('order_id').notNullable().comment('订单id');
          t.tinyint('number').notNullable().comment('数量');
          t.timestamp('create_time').comment('创建时间');
          t.timestamp('update_time').comment('更新时间');
          t.string('create_by').comment('创建人');
          t.string('update_by').comment('更新人');
          t.tinyint('is_delete').comment('是否删除');
        });


        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }
      return str;
    } catch (error) {
      console.log(error);
      return `${table_name} create failed`;
    }
  }

  // 创建订单流程表
  async createOrderProcess(knex) {
    const table_name = 'order_process';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('订单流程表');

          t.string('uuid').notNullable().primary()
            .comment('uiid');
          t.string('order_id').notNullable().comment('订单id');
          t.tinyint('status').notNullable().comment('状态: 0. 等待操作, 1.完成 2.拒绝');
          t.tinyint('type').notNullable().comment('类型: 1.创建订单 2.分拣产品, 3.打包运输, 4.完成订单, 5.取消订单, 6.修改订单');
          t.string('remark').comment('备注');
          t.string('title').comment('标题');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').notNullable().defaultTo(0)
            .comment('是否删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }
      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建订单追踪表
  async createOrderPosition(knex) {
    const table_name = 'order_position';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('订单追踪表');

          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('order_id').notNullable().comment('订单id');
          t.string('position').notNullable().comment('地址');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }
      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建文件表
  async createFile(knex) {
    const table_name = 'file';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('文件表');
          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('name').comment('文件名');
          t.string('url').comment('地址');
          t.string('type').comment('文件类型');
          t.string('size').comment('文件大小');
          t.string('file_cloud').comment('文件位置');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').notNullable().defaultTo(0)
            .comment('是否删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }
      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }


  // 创建文件服务表
  async createFileCloud(knex) {
    const table_name = 'file_cloud';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('文件存储表');
          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('name').notNullable().comment('存储名称');
          t.string('host').notNullable().comment('存储主机地址');
          t.string('port').notNullable().comment('存储主机端口');
          t.string('path').notNullable().comment('存储位置');
          t.jsonb('info').notNullable().comment('存储关键位置');
          t.string('type').notNullable().comment('存储类型');
          t.tinyint('default').notNullable().defaultTo(1)
            .comment('默认上传云');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').notNullable().defaultTo(0)
            .comment('是否删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }
      return str;
    } catch (error) {
      return `${table_name} create failed`;
    }
  }

  // 创建权限表
  async createPermission(knex) {
    const table_name = 'permission';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.comment('权限表');
          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('method').notNullable().comment('请求方式');
          t.string('model').notNullable().comment('模块');
          t.string('name').notNullable().comment('名称');
          t.string('key').notNullable().comment('关键key');
          t.string('url').notNullable().comment('地址');
          t.integer('type').notNullable().comment('类型,1:后端 2:前端');
          t.integer('status').notNullable().defaultTo(0)
            .comment('是否启用： 0:否 1是');
          t.string('parent_id').notNullable().comment('父id');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').notNullable().defaultTo(0)
            .comment('是否删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
          t.unique('key');
          t.unique('url');
        });
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      throw error;
    }
  }

  // 创建角色权限关系表
  async createRoleAndPermission(knex) {
    const table_name = 'role_permission_relationship';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.increments('id').primary().comment('id');
          t.string('r_id').notNullable().comment('角色id');
          t.string('p_id').notNullable().comment('权限id');
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').notNullable().defaultTo(0)
            .comment('是否删除');
        });

        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      throw error;
    }
  }

  async createNotice(knex) {
    const table_name = 'notice';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('title').notNullable().comment('标题');
          t.string('content').notNullable().comment('内容');
          t.string('parent_id').notNullable().comment('父id')
            .defaultTo(0);
          t.timestamp('create_time').defaultTo(knex.fn.now()).comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now()).comment('更新时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').notNullable().defaultTo(0)
            .comment('是否删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });
        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      throw error;
    }
  }

  async createNoticeRead(knex) {
    const table_name = 'notice_read';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('notice_id').notNullable().comment('消息id');
          t.string('user_id').notNullable().comment('用户id');
        });


        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }


      return str;
    } catch (error) {
      throw error;
    }
  }

  async createRequestHistory(knex) {
    const table_name = 'request_history';
    try {
      let str = table_name;
      const isExist = await knex.schema.hasTable(table_name);
      if (!isExist) {
        await knex.schema.createTable(table_name, t => {
          t.string('uuid').notNullable().primary()
            .comment('uuid');
          t.string('model').notNullable().comment('模块');
          t.string('url').notNullable().comment('请求url');
          t.string('ip').notNullable().comment('请求ip');
          t.string('method').notNullable().comment('请求方式');
          t.string('name').notNullable().comment('请求名称');
          t.text('param').notNullable().comment('请求参数');
          t.text('result').notNullable().comment('返回参数');
          t.tinyint('type').notNullable().defaultTo(0)
            .comment('请求类型 0:成功，1.失败');
          t.timestamp('create_time').defaultTo(knex.fn.now())
            .comment('创建时间');
          t.timestamp('update_time').defaultTo(knex.fn.now())
            .comment('创建时间');
          t.string('create_by').notNullable().comment('创建人');
          t.string('update_by').notNullable().comment('更新人');
          t.tinyint('is_delete').defaultTo(0).comment('是否删除');
        });

        await knex.schema.table(table_name, t => {
          t.unique('uuid');
        });

        str += ' create success';
      } else {
        str += ' is exist, not need create';
      }

      return str;
    } catch (error) {
      throw error;
    }
  }


}

module.exports = HomeService;
